package cn.iocoder.yudao.module.erp.controller.admin.erpSupplierguide;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.util.collection.MapUtils;
import cn.iocoder.yudao.module.erp.controller.admin.purchase.vo.in.ErpPurchaseInRespVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.purchase.ErpPurchaseInDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.purchase.ErpSupplierDO;
import cn.iocoder.yudao.module.erp.service.purchase.ErpSupplierService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;

import cn.iocoder.yudao.module.erp.controller.admin.erpSupplierguide.vo.*;
import cn.iocoder.yudao.module.erp.dal.dataobject.erpSupplierguide.ErpSupplierGuideDO;
import cn.iocoder.yudao.module.erp.service.erpSupplierguide.ErpSupplierGuideService;

@Tag(name = "管理后台 - ERP 供应商指导价")
@RestController
@RequestMapping("/erp/supplier-guide")
@Validated
public class ErpSupplierGuideController {

    @Resource
    private ErpSupplierGuideService supplierGuideService;
    @Resource
    private ErpSupplierService supplierService;

    @PostMapping("/create")
    @Operation(summary = "创建ERP 供应商指导价")
    @PreAuthorize("@ss.hasPermission('erp:supplier-guide:create')")
    public CommonResult<Long> createSupplierGuide(@Valid @RequestBody ErpSupplierGuideSaveReqVO createReqVO) {
        return success(supplierGuideService.createSupplierGuide(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新ERP 供应商指导价")
    @PreAuthorize("@ss.hasPermission('erp:supplier-guide:update')")
    public CommonResult<Boolean> updateSupplierGuide(@Valid @RequestBody ErpSupplierGuideSaveReqVO updateReqVO) {
        supplierGuideService.updateSupplierGuide(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除ERP 供应商指导价")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:supplier-guide:delete')")
    public CommonResult<Boolean> deleteSupplierGuide(@RequestParam("id") Long id) {
        supplierGuideService.deleteSupplierGuide(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除ERP 供应商指导价")
                @PreAuthorize("@ss.hasPermission('erp:supplier-guide:delete')")
    public CommonResult<Boolean> deleteSupplierGuideList(@RequestParam("ids") List<Long> ids) {
        supplierGuideService.deleteSupplierGuideListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得ERP 供应商指导价")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:supplier-guide:query')")
    public CommonResult<ErpSupplierGuideRespVO> getSupplierGuide(@RequestParam("id") Long id) {
        ErpSupplierGuideDO supplierGuide = supplierGuideService.getSupplierGuide(id);
        return success(BeanUtils.toBean(supplierGuide, ErpSupplierGuideRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得ERP 供应商指导价分页")
    @PreAuthorize("@ss.hasPermission('erp:supplier-guide:query')")
    public CommonResult<PageResult<ErpSupplierGuideRespVO>> getSupplierGuidePage(@Valid ErpSupplierGuidePageReqVO pageReqVO) {
        PageResult<ErpSupplierGuideDO> pageResult = supplierGuideService.getSupplierGuidePage(pageReqVO);
        return success(buildPageResult(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出ERP 供应商指导价 Excel")
    @PreAuthorize("@ss.hasPermission('erp:supplier-guide:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportSupplierGuideExcel(@Valid ErpSupplierGuidePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        PageResult<ErpSupplierGuideDO> pageResult  = supplierGuideService.getSupplierGuidePage(pageReqVO);
        List<ErpSupplierGuideRespVO> list = buildPageResult(pageResult).getList();
        // 导出 Excel
        ExcelUtils.write(response, "ERP 供应商指导价.xls", "数据", ErpSupplierGuideRespVO.class,
                        BeanUtils.toBean(list, ErpSupplierGuideRespVO.class));
    }

    PageResult<ErpSupplierGuideRespVO> buildPageResult(PageResult<ErpSupplierGuideDO> pageResult) {
        if (CollUtil.isEmpty(pageResult.getList())) {
            return PageResult.empty(pageResult.getTotal());
        }
        // 1.3 供应商信息
        Map<Long, ErpSupplierDO> supplierMap = supplierService.getSupplierMap(
                convertSet(pageResult.getList(), ErpSupplierGuideDO::getSupplierId));

        return BeanUtils.toBean(pageResult, ErpSupplierGuideRespVO.class, VO -> {
            MapUtils.findAndThen(supplierMap, VO.getSupplierId(), supplier -> VO.setSupplierName(supplier.getName()));
        });
    }



}
